<?
	require_once('lib/OwnedObject.php');

	class CarDeveloper extends OwnedObject {
		protected $modelName = 'CarDeveloper';
		protected $alias='';
		protected $name='';

		public static function Install(){
			OwnedObject::Install();

			$query = sprintf("CREATE TABLE `CarDeveloper` (
							`Id` INT( 32 ) unsigned NOT NULL ,
							`alias` VARCHAR( 50 ) NOT NULL ,
							`name` VARCHAR( 50 ) NOT NULL ,
							PRIMARY KEY ( `Id` ) ,
							UNIQUE (`alias` ,`name`	),
							FOREIGN KEY (`Id`) REFERENCES `Object`(`id`) ON DELETE CASCADE
							) ENGINE=INNODB DEFAULT CHARSET=utf8;
						");

			//file_put_contents('dump-CarDeveloper.sql', $query);
			mysql_query($query);

			$query = sprintf("INSERT INTO `ObjectType` SET `name` = '%s'", strval(__CLASS__));
            mysql_query($query);
			echo "CarDeveloper installed\n";
		}

		public function Load(){
			parent::Load();

			$query = sprintf("SELECT `alias`, `name`
							FROM `CarDeveloper`
							WHERE `Id`=%d", $this->id);
			$row=mysql_query($query);
			$rez=mysql_fetch_array($row);

			$this->alias=$rez[0];
			$this->name=$rez[1];

			}
		public function AsSource($otherName='CarDeveloper'){
			$rezult = parent::AsSource($otherName);
			$rezult->Attribute('alias', $this->alias);
			$rezult->Attribute('name', $this->name);


			return $rezult;
		}

		public function CanEdit($user=false){
			if(!$user){
        		if($_SESSION['userid']<1) throw new Exception("No authorized user.");
        		$user = new User($_SESSION['userid']);
        		$user->Load();
        	}
   		    return  $user->Status() == USER_STATUS_ADMIN;
        }

        public function Alias($newAlias=false) {
                        if($newAlias){
                                $this->alias = $newAlias;
						}
                        return $this->alias;
            }

        public function Name($newName=false) {
                        if($newName){
                                $this->name = $newName;
						}
                        return $this->name;
            }

        public function RegisterObject(){
						$obj_id= parent::RegisterObject();
						$query=sprintf("INSERT INTO
												`CarDeveloper` ( `Id` , `alias` , `name`)
											VALUES (%d, '%s', '%s')
							", $obj_id, mes($this->alias), mes($this->name));
						mysql_query($query);
                        if(mysql_errno()) {
                                $this->Remove();
                                throw new DeveloperDuplicateException("Trying register exists developer");
                        }
			}

                public function Save() {
                        parent::Save();
                        $query = sprintf("
                                        UPDATE `CarDeveloper`
                                        SET
                                                `alias`='%s',
                                                `name`='%s',
                                        WHERE
                                                `Id`=%d",
                                        mes($this->alias), mes($this->name), $this->id
                                );
                        mysql_query($query);
                }

		public function Remove(){
				$query=sprintf("DELETE FROM `CarDeveloper` WHERE Id=%d",$this->id);
				mysql_query($query);
				parent::Remove();
			}
			
		public static function AllDevelopers(){
		
			$query = "SELECT `Id` FROM `CarDeveloper`";
			$parse = mysql_query($query);
		
			$cars = new XmlSource('all-developers');
			while($result = mysql_fetch_assoc($parse)){
				$item = new CarDeveloper($result['Id']);
				$item->Load();
				$cars->MountSource($item->AsSource());
			}
			
			return $cars;
		
		}				

	}
	


    class DeveloperException extends Exception {
    }

    class DeveloperDuplicateException extends DeveloperException {
    }


?>
